#include "StdAfx.h"
#include "NetInfo.h"
#include <malloc.h>
//#include <Strsafe.h>
#include <stdio.h>
#include <IPHlpApi.h>
#pragma comment(lib,"Iphlpapi.lib")
//#define REMOTE_TEST 
CNetInfo::CNetInfo(void):
m_pAdapterInfo(NULL),
m_nMacNum(-1)
{
}

CNetInfo::~CNetInfo(void)
{
}
BOOL CNetInfo::Initialize()
{
	ZeroMemory(&m_pCurrentMacAddr,sizeof(m_pCurrentMacAddr));
	//.ZeroMemory(&m_pCurrentIPAddr,sizeof(m_pCurrentIPAddr));
	ULONG uAdapterInfoSize = 0;
	GetAdaptersInfo(NULL,&uAdapterInfoSize);
	m_pAdapterInfo = (PIP_ADAPTER_INFO)malloc(uAdapterInfoSize);
	ZeroMemory(m_pAdapterInfo,uAdapterInfoSize);
	if (NO_ERROR!=GetAdaptersInfo(m_pAdapterInfo,&uAdapterInfoSize))
	{
		return FALSE;
	}
	CalculateMacNumber();
	return TRUE;
}
BOOL CNetInfo::UnInitialize()
{
	if (m_pAdapterInfo)
	{
		free(m_pAdapterInfo);
		m_pAdapterInfo = NULL;
	}
	ZeroMemory(&m_pCurrentMacAddr,sizeof(m_pCurrentMacAddr));
	return TRUE;
}
CString CNetInfo::GetHostName()
{
	CString strHostName = _T("");
	char chHostname[512+2]={0};
	if(0==gethostname(chHostname,512))
	{
		strHostName = chHostname;
	}
	return strHostName;
}
const char * CNetInfo::GetEthernetIPAddressEx()
{
	char chHostname[512+2]={0};
	if(0==gethostname(chHostname,512))
	{
		struct hostent *phe = gethostbyname(chHostname);
		if (phe)
		{
			return inet_ntoa (*(struct in_addr *)*phe->h_addr_list);
		}
	}
	return NULL;
}
INT  CNetInfo::GetIPNumber(INT nMacIndex)
{
	PIP_ADAPTER_INFO pAdapter = GetAddapterInfoByIndex(nMacIndex);
	INT nIPNumber = 0;
	if (pAdapter)
	{
		PIP_ADDR_STRING pIPAddr = &pAdapter->IpAddressList;
		while (pIPAddr)
		{
			nIPNumber++;
			pIPAddr = pIPAddr->Next;
		}
	}
	return nIPNumber;
}
const char * CNetInfo::GetDefaultIP(INT nMacIndex)
{
	return GetIPByIndex(0,nMacIndex);
}
const char * CNetInfo::GetIPByIndex(INT nIndex,INT nMacIndex)
{
	PIP_ADAPTER_INFO pAdapter = GetAddapterInfoByIndex(nMacIndex);
	if (pAdapter)
	{
		PIP_ADDR_STRING pIPAddr = &pAdapter->IpAddressList;
		for (INT i=0; pIPAddr &&(i<nIndex);i++)
		{
			pIPAddr = pIPAddr->Next;
		}
		if (pIPAddr)
		{
			return pIPAddr->IpAddress.String;
		}

	}
	return NULL;
}
INT CNetInfo::GetMacNumber()
{
	return m_nMacNum;
}
VOID CNetInfo::CalculateMacNumber()
{
	m_nMacNum = 0;
	PIP_ADAPTER_INFO pAdapterInfo = m_pAdapterInfo;
	while (pAdapterInfo)
	{
		m_nMacNum++;
		pAdapterInfo = pAdapterInfo->Next;
	}
}
PIP_ADAPTER_INFO CNetInfo::GetAddapterInfoByIndex(INT nIndex)
{
	PIP_ADAPTER_INFO pAdapterInfo = m_pAdapterInfo;
	for (int i=0;pAdapterInfo && (i< nIndex);i++)
	{
		pAdapterInfo = pAdapterInfo->Next;
	}
	return pAdapterInfo;
}
DWORD CNetInfo::GetMacType(INT nIndex)
{
	PIP_ADAPTER_INFO pAdapterInfo = GetAddapterInfoByIndex(nIndex);
	if (pAdapterInfo)
	{
		return pAdapterInfo->Type;
	}
	return - 1;//MIB_IF_TYPE_ETHERNET 
}
PIP_ADAPTER_INFO CNetInfo::GetEthernetAdapterInfo()
{
	PIP_ADAPTER_INFO pAdapterInfo = m_pAdapterInfo;
	while (pAdapterInfo!=NULL)
	{
		if (MIB_IF_TYPE_ETHERNET==pAdapterInfo->Type)
		{
			return pAdapterInfo;
		}
		pAdapterInfo = pAdapterInfo->Next;
	}
	return NULL;
}
INT CNetInfo::GetEthernetMacIndex()
{
	INT nAdapterNum = this->GetMacNumber();
	for (INT i=0;i<nAdapterNum;i++)
	{
		if (MIB_IF_TYPE_ETHERNET == this->GetMacType(i))
		{
			return  i;
		}
	}
	return -1;
}
const char * CNetInfo::GetEthernetMacAddress()
{
	INT nAdapterNum = this->GetMacNumber();
	for (INT i=0;i<nAdapterNum;i++)
	{
		if (MIB_IF_TYPE_ETHERNET == this->GetMacType(i))
		{
			return this->GetMacAddress(i);
		}
	}
	return NULL;
}
const char * CNetInfo::GetEthernetIPAddress()
{
// 	PIP_ADAPTER_INFO pAdapterInfo = this->GetEthernetAdapterInfo();
// 	if (pAdapterInfo)
// 	{
// #ifdef REMOTE_TEST
// 		return "59.68.1.202";
// #endif
// 		return pAdapterInfo->IpAddressList.IpAddress.String;
// 	}
#if 1
	return GetEthernetIPAddressEx();
#endif
	PIP_ADAPTER_INFO pAdapterInfo = m_pAdapterInfo;
	while (pAdapterInfo!=NULL)
	{
		if (MIB_IF_TYPE_ETHERNET==pAdapterInfo->Type)
		{
			if (0!=strcmp(pAdapterInfo->IpAddressList.IpAddress.String,"0.0.0.0"))
			{
				return pAdapterInfo->IpAddressList.IpAddress.String;
			}
		}
		pAdapterInfo = pAdapterInfo->Next;
	}
	return NULL;
	return NULL;
}
const char * CNetInfo::GetMacAddress(INT nIndex )
{
	PIP_ADAPTER_INFO pAdapterInfo = GetAddapterInfoByIndex(nIndex);
	if (NULL==pAdapterInfo)
	{
		return 0;
	}
	INT j=0;
	for (UINT i = 0; i < pAdapterInfo->AddressLength; i++,j+=2)
	{
		//sprintf(&m_pCurrentMacAddr[j],"%.2x",(int)pAdapterInfo->Address[i]);
		//		sprintf_s(&m_pCurrentMacAddr[j],3,"%.2X",(int)pAdapterInfo->Address[i]);
		m_pCurrentMacAddr[i] = pAdapterInfo->Address[i];
	}
	m_pCurrentMacAddr[j-1]= '\0';
	m_pCurrentMacAddr[j]= '\0';
	return m_pCurrentMacAddr;
}